package com.bohan.main;


import com.bohan.hit.Hit;
import com.bohan.katz.Katz;
import com.bohan.simpage.SimPage;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;





import Jama.Matrix;

public class Main {
	public static int edges = 0;
	public static double beta = 0.3;
	public static double gamma = 0.8;
	public static double alpha = 0.3;
	public static int topN = 5, step = 100, depth = 10, datarange = 300, iter = 5;
	public static int randommax;
	public static String filePrefix, fileIn, fileOut,fileResKat, fileResSim, fileResRand;
	public static String json = "table.json";
	public static HashMap <String ,Integer > stoi = new HashMap <String ,Integer >() ;
	public static HashMap <Integer, String> itos = new HashMap <Integer, String>();
	public static void setJason2Read(String path)
	{
		fileIn = path;
		int index = path.lastIndexOf(File.separator);
		if(index == -1)
			filePrefix = "";
		else
			filePrefix = path.substring(0,path.substring(0,path.substring(0, index).lastIndexOf(File.separator)).lastIndexOf(File.separator)) 
			+ File.separator+"libohan"+File.separator+"data"+File.separator;
	}
	public static String katzJason()
	{
		return fileResKat;
	}
	public static String simJason()
	{
		return fileResSim;
	}
	public static String randJason()
	{
		return fileResRand;
	}
	//public static void main(String args[]) throws IOException, JSONException
	public static String linkPredPart2(double betaTe, int topk) throws IOException, JSONException
	{
		edges = 0;
		beta = 0.3;
		gamma = 0.8;
		alpha = 0.3;
		topN = 5; step = 100; depth = 10; datarange = 300; iter = 5;
		json = "table.json";
		stoi = new HashMap <String ,Integer >() ;
		itos = new HashMap <Integer, String>();
	//	setJason2Read("top_k_data.json");
	//	String algo = "randomwalk";
	//	filePrefix = "duansky/data/";
	//	fileIn = filePrefix + "data.json"; 
		//fileIn = "libohan/";
		//System.out.println(fileIn + "ddd" );
		if(fileIn.equals(""))
			return "";
		beta = betaTe;
		//topN ;
		fileOut = filePrefix + "data_in.txt";
		//System.out.println(fileOut );
		fileResKat = filePrefix + "data_katz.json"; 
		int showResult = topk;
		PreReader pr = new PreReader();
		int size = pr.preRead(fileIn,fileOut) + 1;
		
		size = size > (datarange + 1) ? (datarange + 1): size;
		datarange = size;

//		if(algo.equals("katz")){
			Katz kat = new Katz();
			kat.setMatrix(size);
			Matrix matrix = kat.readFile(fileOut);
			Matrix matrixI = kat.getI();
			Matrix matrixResult = kat.compute(matrix, matrixI);
			
			matrix = null;
			matrixI = null;
			String res = kat.show(matrixResult, showResult,fileIn, fileResKat);
			kat.clear();
			matrixResult = null;
//		}

		return res;
	}
	
	public static String linkPredPart2_hit(int stepte, int topk) throws IOException, JSONException
	{
	//	setJason2Read("top_k_data.json");
	//	String algo = "randomwalk";
	//	filePrefix = "duansky/data/";
	//	fileIn = filePrefix + "data.json"; 
		//fileIn = "libohan/";
		//System.out.println(fileIn + "ddd" );
		edges = 0;
		beta = 0.3;
		gamma = 0.8;
		alpha = 0.3;
		topN = 5; step = 100; depth = 10; datarange = 300; iter = 5;
		json = "table.json";
		stoi = new HashMap <String ,Integer >() ;
		itos = new HashMap <Integer, String>();
		if(fileIn.equals(""))
			return "";
		step = stepte;
		//topN = topk;
		fileOut = filePrefix + "data_in.txt";
		fileResRand = filePrefix + "data_rand.json";
		int showResult = topk;
		PreReader pr = new PreReader();
		int size = pr.preRead(fileIn,fileOut) + 1;
		
		size = size > (datarange + 1) ? (datarange + 1): size;
		datarange = size;
		
			Hit hit = new Hit(size);
			hit.setNeigh(size);
			hit.readFile(fileOut);
			Vector <Integer> [] neigh = hit.getNeighIn();
			hit.compute(neigh, size);
			neigh = hit.getNeighOut();
			hit.compute(neigh, size);
			String res = hit.show(showResult, fileIn,fileResRand);
			hit.clear(neigh);

		return res;
	}
	
	
	public static String linkPredPart2_sim(double gammaTe, int iterTe, int topk) throws IOException, JSONException
	{
	//	setJason2Read("top_k_data.json");
	//	String algo = "randomwalk";
	//	filePrefix = "duansky/data/";
	//	fileIn = filePrefix + "data.json"; 
		//fileIn = "libohan/";
		//System.out.println(fileIn + "ddd" );
		edges = 0;
		beta = 0.3;
		gamma = 0.8;
		alpha = 0.3;
		topN = 5; step = 100; depth = 10; datarange = 300; iter = 5;
		json = "table.json";
		stoi = new HashMap <String ,Integer >() ;
		itos = new HashMap <Integer, String>();
		if(fileIn.equals(""))
			return "";
		gamma = gammaTe;
		iter =  iterTe;
		//topN = topk;
		fileOut = filePrefix + "data_in.txt";
		//System.out.println(fileOut );
		fileResSim = filePrefix + "data_sim.json"; 
		
		int showResult = topk;
		PreReader pr = new PreReader();
		int size = pr.preRead(fileIn,fileOut) + 1;
		
		size = size > (datarange + 1) ? (datarange + 1): size;
		datarange = size;
		
			SimPage simpa = new SimPage(size);
			simpa.setNeigh(size);
			simpa.readFile(fileOut);
			ArrayList <Integer> [] neighOut = simpa.getNeighOut();
			ArrayList <Integer> [] neighIn = simpa.getNeighIn();
			simpa.computeIn( neighIn, size);
			String res = simpa.showIn(showResult,fileIn, fileResSim);
			simpa.clear(neighOut, neighIn);
		return res;
	}
}
